package com.minshenglife.zanzan.timer;

import javax.annotation.Resource;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.minshenglife.zanzan.entity.AccessTokenAndJsapiTicket;
import com.minshenglife.zanzan.pub.Config;
import com.minshenglife.zanzan.pub.utils.HttpUtil;
import com.minshenglife.zanzan.service.AccessTokenAndJsapiTicketService;

/**
 * 定时器 执行 用于获取AccessToken和Jsapi_Ticket,并保存到数据库中
 * 
 * @author wangshuzhi
 *
 */
public class AccessTokenAndJsapiTicketTimer {
	Logger log = LoggerFactory.getLogger(AccessTokenAndJsapiTicketTimer.class);

	@Resource
	AccessTokenAndJsapiTicketService service;

	public void getWeiXin() {
		log.info("======AccessTokenAndJsapiTicketTimer.getWeiXin() start");
		try {
			AccessTokenAndJsapiTicket ticket = null;
			String appid = Config.getProperty("WX_APPID");
			
			ticket = service.getAccessTokenAndJsapiTicket(appid);
			
			boolean flag = true;
			if (ticket == null) {
				log.info("ticket is null"+ticket);
				ticket = new AccessTokenAndJsapiTicket();
				ticket.setAppid(Config.getProperty("WX_APPID"));
				flag = false;
			}
			
			
			
			String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
			access_token_url = access_token_url.replace("APPID",
					Config.getProperty("WX_APPID")).replace("APPSECRET",
					Config.getProperty("WX_SECRET"));
			log.info("请求微信获取accesstoken接口连接-->" + access_token_url);
			JSONObject jsonObject = HttpUtil.httpsRequest(access_token_url,
					"GET", null);
			log.info("请求微信获取accesstoken接口返回结果-->" + jsonObject.toString());
			if (null != jsonObject) {
				if (jsonObject.containsKey("access_token")) {
					log.info("获取access_token成功-->"
							+ jsonObject.getString("access_token"));
					String jsapiUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
					String jsapiParam = "access_token=ACCESS_TOKEN&type=jsapi";
					jsapiParam = jsapiParam.replace("ACCESS_TOKEN",
							jsonObject.getString("access_token"));
					System.out.println("请求微信获取jsapi_ticket接口连接-->" + jsapiUrl
							+ "?" + jsapiParam);
					log.info("请求微信获取jsapi_ticket接口连接-->" + jsapiUrl + "?"
							+ jsapiParam);
					String js_api_ticket_str = HttpUtil.sendGet(jsapiUrl,
							jsapiParam);
					System.out.println("请求微信获取jsapi_ticket接口返回结果-->"
							+ js_api_ticket_str);
					log.info("请求微信获取jsapi_ticket接口返回结果-->" + js_api_ticket_str);
					if (js_api_ticket_str != null
							& !js_api_ticket_str.trim().equals("")) {
						JSONObject jsApiTicketJson = JSONObject
								.fromObject(js_api_ticket_str);
						if (jsApiTicketJson.containsKey("errcode")
								& jsApiTicketJson.getInt("errcode") == 0) {
							log.info("请求微信获取jsApiTicketJson成功-->"
									+ jsApiTicketJson.getString("ticket"));
							ticket.setAccessToken(jsonObject
									.getString("access_token"));
							ticket.setJsapiTicket(jsApiTicketJson
									.getString("ticket"));
							ticket.setExpiresIn(jsApiTicketJson
									.getString("expires_in"));
							ticket.setCreatetime(String.valueOf(System
									.currentTimeMillis()));
							boolean isSuccess = false;
							if (flag) {
								isSuccess = service
										.updateAccessTokenAndJsapiTicket(ticket);
							} else {
								isSuccess = service
										.insertAccessTokenAndJsapiTicket(ticket);
							}
							if (isSuccess) {
								log.info("jsapiticket 和 accesstoken保存成功。。。");
							} else {
								log.error("jsapiticket 和 accesstoken保存失败！！！");
							}
						} else {
							log.error("请求微信获取jsApiTicketJson失败-->"
									+ jsApiTicketJson.getInt("errcode") + ":"
									+ jsApiTicketJson.getString("errmsg"));
						}
					} else {

					}

				} else {
					log.error("调用access_token接口失败=="
							+ jsonObject.getString("errcode") + ":"
							+ jsonObject.getString("errmsg"));
				}
			}
//			else {
//				log.error("调用access_token接口失败，未返回任何数据");
//			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		log.info("======AccessTokenAndJsapiTicketTimer.getWeiXin() end");
	}

	public static void main(String[] args) {
		new AccessTokenAndJsapiTicketTimer().getWeiXin();
	}
}
